<?php

namespace App\Http\Controllers;

use App\Model\Student;
use Illuminate\Http\Request;

class PersonController extends Controller
{
    //个人中心模板渲染
    public function person(){
        $userid = \Cache::get($_COOKIE['userid']);
        $user = Student::where('userid',$userid)->first();
        $books = $user->books()->get();
        return view('person',['user' => $user,'books' => $books]);
    }

    //还书
    public function returnBook($id){
        $userid = \Cache::get($_COOKIE['userid']);
        $user = Student::where('userid',$userid)->first();
        $user->books()->detach($id);
        die('还书成功！');
    }

    //续借
    public function addTime($id){
        $userid = \Cache::get($_COOKIE['userid']);
        $user = Student::where('userid',$userid)->first();
        $user->books()->updateExistingPivot($id, ['return_date' => date('Y-m-d',time()+2595600)]);
        die('续借成功！');
    }
}
